package com.mindata.blockchain.socket.distruptor;

import com.lmax.disruptor.RingBuffer;
import com.mindata.blockchain.socket.distruptor.base.BaseEvent;
import com.mindata.blockchain.socket.distruptor.base.MessageProducer;

/**
 * 所有客户端、server端发来的消息，都进入这里，然后publish出去，供消费者消费
 * @author wuweifeng wrote on 2018/4/20.
 */
public class DisruptorProducer implements MessageProducer {
	private RingBuffer<BaseEvent> ringBuffer;

	public DisruptorProducer(RingBuffer<BaseEvent> ringBuffer) {
		this.ringBuffer = ringBuffer;
	}

	@Override
	public void publish(BaseEvent baseEvent) {
		long sequence = this.ringBuffer.next();
		BaseEvent event = this.ringBuffer.get(sequence);
		event.setBlockPacket(baseEvent.getBlockPacket());
		event.setChannelContext(baseEvent.getChannelContext());
		this.ringBuffer.publish(sequence);
	}
}
